//
//  ReceiveResume.m
//  HappyStore
//
//  Created by 点点充 on 16/1/28.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "ReceiveResume.h"
#import "RecruitmentInfo.h"
#import "LookResume.h"
#import "thirdPersonVC.h"
@interface ReceiveResume ()
{
    NSMutableArray *muAryInfo;
    NSInteger paggge;
    
    NSMutableDictionary *muDicParameters;
    
}
@end

@implementation ReceiveResume
@synthesize mainType;

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = NO;
    
    if (muDicParameters == nil) {
        muDicParameters = [NSMutableDictionary dictionary];
        paggge = 1;
        [muDicParameters setObject:[NSString stringWithFormat:@"%ld",paggge] forKey:@"page"];
    }

    [self startSearch];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
    //找出导航栏的下边的黑线
    UIImageView *navBarHairlineImageView = [NaviBarLine findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = NO;
    navBarHairlineImageView.alpha = 0.5f;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    //找出导航栏的下边的黑线
    UIImageView *navBarHairlineImageView = [NaviBarLine findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = NO;
    navBarHairlineImageView.alpha = 0.5f;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    switch (mainType) {//1:个人、投递记录    2:企业、邀请投递记录 3:个人、被邀请纪录   4:企业、收到的简历
        case 1:
        {
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
            titleLabel.backgroundColor = [UIColor clearColor];
            titleLabel.font = [UIFont systemFontOfSize:19];
            titleLabel.text = @"投递记录";
            titleLabel.textColor = RGB(45, 45, 45);
            titleLabel.textAlignment = NSTextAlignmentCenter;
            self.navigationItem.titleView = titleLabel;
            
        }
            break;
        case 2:
        {
            self.navigationItem.title = @"邀请投递记录";
        }
            break;
        case 3:
        {
            self.navigationItem.title = @"被邀请纪录";
        }
            break;
        case 4:
        {
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
            titleLabel.backgroundColor = [UIColor clearColor];
            titleLabel.font = [UIFont systemFontOfSize:19];
            titleLabel.text = @"收到的简历";
            titleLabel.textColor = RGB(45, 45, 45);
            titleLabel.textAlignment = NSTextAlignmentCenter;
            self.navigationItem.titleView = titleLabel;
            
        }
            break;
    }
    
    _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (muAryInfo.count != 0) {
            NSString *path = [[NSBundle mainBundle]pathForResource:@"refresh"
                                                            ofType:@"wav"];
            SystemSoundID soundID;
            AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
            AudioServicesPlaySystemSound(soundID); //仅播放音频
            
            paggge = 1;
            [muDicParameters setObject:@"1" forKey:@"page"];
            
            [self startSearch];
        }else {
            [_mainTableView.mj_header endRefreshing];
        }
    }];
    
    
    
    _mainTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        paggge += 1;
        [muDicParameters setObject:[NSString stringWithFormat:@"%ld",paggge] forKey:@"page"];
        [self startSearch];
    }];
    
    _mainTableView.mj_footer.hidden = YES;
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
}
-(void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [tableView tableViewDisplayWitMsg:@"没有查询到相对应的数据" ifNecessaryForRowCount:muAryInfo.count];
    return muAryInfo.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellLoveSingle = @"cellLoveSingle";//首页
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellLoveSingle forIndexPath:indexPath];
    
    UIView *viewBack = [cell viewWithTag:50];
    viewBack.layer.masksToBounds = YES;
    viewBack.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:1.0f].CGColor;
    viewBack.layer.borderWidth = 0.5f;
    
    
    UIImageView *imgHeader = [cell viewWithTag:51];
    [imgHeader sd_setImageWithURL:[NSURL URLWithString:muAryInfo[indexPath.row][@"headPhoto"]] placeholderImage:[UIImage imageNamed:@"img_normal3"]];
    imgHeader.layer.masksToBounds = YES;
    imgHeader.layer.cornerRadius = 23.f;
    
    UILabel *labUserName = [cell viewWithTag:52];
    if (muAryInfo[indexPath.row][@"username"] == [NSNull null] || [muAryInfo[indexPath.row][@"username"] isEqualToString:@"null"]) {
        labUserName.text = muAryInfo[indexPath.row][@"account"];
    }else {
        labUserName.text = muAryInfo[indexPath.row][@"username"];
    }
    
    //进用户详情
    UIButton *btnHeader = [cell viewWithTag:53];
    [btnHeader addTarget:self action:@selector(btnClickUser:) forControlEvents:UIControlEventTouchUpInside];
    cell.contentView.tag = indexPath.row;
    
    UILabel *labTitle = [cell viewWithTag:10];
    labTitle.text = muAryInfo[indexPath.row][@"positionName"];
    
    UILabel *labPrice = [cell viewWithTag:11];
    if ([muAryInfo[indexPath.row][@"money"] isEqualToString:@""]||[muAryInfo[indexPath.row][@"money"] integerValue] == 0) {
        labPrice.text = @"面议";
    }else{
        labPrice.text = [CustomExcetion comperNumber:muAryInfo[indexPath.row][@"money"]];
    }
    UILabel *labPosi = [cell viewWithTag:12];
    labPosi.text = muAryInfo[indexPath.row][@"positionName"];
    
    UILabel *labAddress = [cell viewWithTag:13];
    labAddress.text = muAryInfo[indexPath.row][@"addresss"];
    
    UILabel *labTime = [cell viewWithTag:14];
    labTime.text = [CustomExcetion prettyDateWithReference:[CustomExcetion dateFromString:muAryInfo[indexPath.row][@"createTime"]]];
    
    //查看
    UILabel *labLook = [cell viewWithTag:15];
    labLook.hidden = YES;
    switch (mainType) {//1:个人、投递记录    2:企业、邀请投递记录 3:个人、被邀请纪录   4:企业、收到的简历
        case 1:
        {
            labLook.hidden = NO;
            if ([muAryInfo[indexPath.row][@"reading"] isEqualToString:@"是"]) {
                labLook.text = @"已查看";
            }else {
                labLook.text = @"未查看";
            }
        }
            break;
        case 2:
        {
            labLook.hidden = NO;
            if ([muAryInfo[indexPath.row][@"reading"] isEqualToString:@"是"]) {
                labLook.text = @"已查看";
            }else {
                labLook.text = @"未查看";
            }
        }
            break;
    }
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//取消选中状态
    
    //取消显示后退键文字
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barButtonItem;
    
    if (self.mainType == 4) {//收到的简历
        LookResume *lookResumeVC = [[LookResume alloc] init];
        lookResumeVC.resumeBtnTig = @"resumeBtnTig";
        [self.navigationController pushViewController:lookResumeVC animated:YES];
    }else{
        RecruitmentInfo *recruVC = [[RecruitmentInfo alloc] initWithNibName:@"RecruitmentInfo" bundle:nil];
        recruVC.jobId = muAryInfo[indexPath.row][@"jobId"];
        [self.navigationController pushViewController:recruVC animated:YES];
    }
//    Recruitment_Detail *result = [self.storyboard instantiateViewControllerWithIdentifier:@"recruitment_Detail"];
//    result.projectId = muAryInfo[indexPath.row][@"jobId"];
    
    
//    switch (mainType) {//1:个人、投递记录    2:企业、邀请投递记录 3:个人、被邀请纪录   4:企业、收到的简历
//        case 1:
//        {
//            //招聘详情
//            result.strType = @"1";
//            [self.navigationController pushViewController:result animated:YES];
//        }
//            break;
//        case 2:
//        {
//            //求职详情
//            result.strType = @"2";
//            [self.navigationController pushViewController:result animated:YES];
//        }
//            break;
//        case 3:
//        {
//            //招聘详情
//            result.strType = @"1";
//            [self startPostEditState:indexPath withVC:result];
//        }
//            break;
//        case 4:
//        {
//            //求职详情
//            result.strType = @"2";
//            [self startPostEditState:indexPath withVC:result];
//        }
//            break;
//    }
    
    
    
}


- (void)startSearch
{
    //token=验证用户&userId=用户id&page=页码&type=类型（1 投递 2邀请）
    
    
    [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"token"] forKey:@"token"];//验证用户
    [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"userid"] forKey:@"userId"];
    NSString *strURl;
    switch (mainType) {//1:个人、投递记录    2:企业、邀请投递记录 3:个人、被邀请纪录   4:企业、收到的简历
        case 1:
        {
            strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"getMyResume_TouYaoHou_app_myHome.im.lensum"];
            [muDicParameters setObject:@"1" forKey:@"type"];
        }
            break;
        case 2:
        {
            strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"getMyResume_TouYaoHou_app_myHome.im.lensum"];
            [muDicParameters setObject:@"2" forKey:@"type"];
        }
            break;
        case 3:
        {
            strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"getMyResume_app_myHome.im.lensum"];
            [muDicParameters setObject:@"2" forKey:@"type"];
        }
            break;
        case 4:
        {
            strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"getMyResume_app_myHome.im.lensum"];
            [muDicParameters setObject:@"1" forKey:@"type"];
        }
            break;
    }
    
    
    

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:strURl parameters:muDicParameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        //NSLog(@"%@",dicInfo);
        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            
            if (paggge == 1) {
                [_mainTableView.mj_header endRefreshing];
                
                muAryInfo = [NSMutableArray arrayWithArray:[dicInfo objectForKey:@"data"][@"record"]];
                
                if (muAryInfo.count == 10) {
                    _mainTableView.mj_footer.hidden = NO;
                }else {
                    _mainTableView.mj_footer.hidden = YES;
                }
                
            }else {
                [_mainTableView.mj_footer endRefreshing];
                NSArray *ary11 = [dicInfo objectForKey:@"data"][@"record"];
                for (int i = 0; i<ary11.count; i++) {
                    NSDictionary *dic1 = ary11[i];
                    [muAryInfo addObject:dic1];
                }
                if (ary11.count == 10) {
                    _mainTableView.mj_footer.hidden = NO;
                }else {
                    _mainTableView.mj_footer.hidden = YES;
                }
                
            }
            
            
            [_mainTableView reloadData];

        }else {
            [_mainTableView reloadData];
            [_mainTableView.mj_header endRefreshing];
            [_mainTableView.mj_footer endRefreshing];

        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [_mainTableView.mj_header endRefreshing];
        [_mainTableView.mj_footer endRefreshing];


    }];
}


#pragma mark - 改变我收到的 投递/邀请 简历的状态
- (void)startPostEditState:(NSIndexPath *)indexPath withVC:(Recruitment_Detail *)result
{
    NSMutableDictionary *muDicParameters1 = [NSMutableDictionary dictionary];
    //token=rE3F2m8AvfC7CVh85O9q&resumeId=投递邀请的id
    [muDicParameters1 setObject:[USER_DEFAULTS objectForKey:@"token"] forKey:@"token"];//验证用户
    [muDicParameters1 setObject:muAryInfo[indexPath.row][@"resumeId"] forKey:@"resumeId"];//发送招聘求职的id
    
    NSString *strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"changeResumeStatus_app_myHome.im.lensum"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:strURl parameters:muDicParameters1 progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        //NSLog(@"%@",dicInfo);
        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {

            [self.navigationController pushViewController:result animated:YES];
        }else {

           [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];
        }
        

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {


    }];
}

- (void)btnClickUser:(UIButton *)btn
{
    //取消显示后退键文字
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barButtonItem;
    
    NSInteger indexx = btn.superview.superview.tag;
    
    switch (mainType) {//1:个人、投递记录    2:企业、邀请投递记录 3:个人、被邀请纪录   4:企业、收到的简历
        case 1:
        {
//            ThirdPerson_Company *person = [self.storyboard instantiateViewControllerWithIdentifier:@"thirdCompany"];
//            person.strUserId = muAryInfo[indexx][@"userId"];
//            person.strUserType = @"2";//企业
//            person.strUserName = [NSString stringWithFormat:@"%@的详情",muAryInfo[indexx][@"username"]];
//            [self.navigationController pushViewController:person animated:YES];
            thirdPersonVC *personVC = [[thirdPersonVC alloc] init];
            personVC.userId = muAryInfo[indexx][@"userId"];
            personVC.userType = @"2";//企业
            [self.navigationController pushViewController:personVC animated:YES];
            
        }
            break;
        case 2:
        {
//            ThirdPerson_Person *person = [self.storyboard instantiateViewControllerWithIdentifier:@"thirdPerson"];
//            person.strUserId = muAryInfo[indexx][@"userId"];
//            person.strUserType = @"1";//个人
//            person.strUserName = [NSString stringWithFormat:@"%@的详情",muAryInfo[indexx][@"username"]];
//            [self.navigationController pushViewController:person animated:YES];
            thirdPersonVC *personVC = [[thirdPersonVC alloc] init];
            personVC.userId = muAryInfo[indexx][@"userId"];
            personVC.userType = @"1";//个人
            [self.navigationController pushViewController:personVC animated:YES];
        }
            break;
        case 3:
        {
//            ThirdPerson_Company *person = [self.storyboard instantiateViewControllerWithIdentifier:@"thirdCompany"];
//            person.strUserId = muAryInfo[indexx][@"userId"];
//            person.strUserType = @"2";//企业
//            person.strUserName = [NSString stringWithFormat:@"%@的详情",muAryInfo[indexx][@"username"]];
//            [self.navigationController pushViewController:person animated:YES];
            thirdPersonVC *personVC = [[thirdPersonVC alloc] init];
            personVC.userId = muAryInfo[indexx][@"userId"];
            personVC.userType = @"2";//企业
            [self.navigationController pushViewController:personVC animated:YES];
        }
            break;
        case 4:
        {
//            ThirdPerson_Person *person = [self.storyboard instantiateViewControllerWithIdentifier:@"thirdPerson"];
//            person.strUserId = muAryInfo[indexx][@"userId"];
//            person.strUserType = @"1";//个人
//            person.strUserName = [NSString stringWithFormat:@"%@的详情",muAryInfo[indexx][@"username"]];
//            [self.navigationController pushViewController:person animated:YES];
            thirdPersonVC *personVC = [[thirdPersonVC alloc] init];
            personVC.userId = muAryInfo[indexx][@"userId"];
            personVC.userType = @"1";//个人
            [self.navigationController pushViewController:personVC animated:YES];
        }
            break;
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
